package com.shop.zkmall.app.utils;


import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ses.v20201002.SesClient;
import com.tencentcloudapi.ses.v20201002.models.SendEmailRequest;
import com.tencentcloudapi.ses.v20201002.models.SendEmailResponse;
import com.tencentcloudapi.ses.v20201002.models.Template;

public class TencentCloudEmailUtil {

    // 必填：你的 SecretId 和 SecretKey（建议配置在 application.yml）
    private static final String SECRET_ID = "AKID99xZ4c0LdonaCcR4VkmaBdsyr00wqa7T";
    private static final String SECRET_KEY = "DE4kGZgcWEj040jFdpoa8WhgS1NVTEl5";

    // 地域，固定为 ap-hongkong 或其他你 SES 服务开通的区域
    private static final String REGION = "ap-hongkong";

    // 发信地址（腾讯云 SES 控制台创建）
    private static final String FROM_EMAIL = "livemall@talktheprice.com";


    public static void main(String[] args) {
        boolean result = sendTemplateEmail("905029404@qq.com",
                156777, "{\"name\": \"张三\", \"code\": \"123456\"}", "年");
    }

    /**
     * 使用模板发送邮件（可传变量）
     * 模板变量需格式： {"name": "张三", "code": "123456"}
     */
    public static boolean sendTemplateEmail(String toEmail, long templateId, String templateDataJson, String subject) {
        try {
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ses.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SesClient client = new SesClient(cred, REGION, clientProfile);

            SendEmailRequest req = new SendEmailRequest();
            req.setFromEmailAddress(FROM_EMAIL);
            req.setDestination(new String[]{toEmail});
            req.setSubject(subject);
            req.setReplyToAddresses(FROM_EMAIL);

            Template template = new Template();
            template.setTemplateID(templateId);
            template.setTemplateData(templateDataJson);

            req.setTemplate(template);

            SendEmailResponse res = client.SendEmail(req);
            System.out.println("模板邮件发送成功：" + SendEmailResponse.toJsonString(res));
            return true;

        } catch (TencentCloudSDKException e) {
            System.err.println("模板邮件发送失败：" + e.getMessage());
            return false;
        }
    }


}
